<template>
  <el-dialog
    title="分配角色"
    :visible.sync="visible"
    :before-close="close"
    :append-to-body="true"
    width="500px"
  >
    <el-form :model="form" size="small" label-width="120px">
      <el-form-item label="角色名称：">
        <el-input
          type="text"
          v-model="newForm.name"
          placeholder="请输入角色名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="描述：">
        <el-input
          type="textarea"
          :rows="3"
          v-model="newForm.description"
          placeholder="请输入描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否启用：">
        <el-radio v-model="newForm.status" :label="1">是</el-radio>
        <el-radio v-model="newForm.status" :label="0">否</el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="close('reload')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRoleLists, addRoleInfo, updateRoleInfo } from "@/request/api";
export default {
  props: {
    visible: {
      default: false,
      type: Boolean,
    },
    form: {
      default: {},
      type: Object,
    },
  },
  data() {
    return {
      newForm: {},
    };
  },
  watch: {
    form(newVal, oldVal) {
      this.newForm = JSON.parse(JSON.stringify(newVal));
    },
  },
  methods: {
    fetchRoleLists() {
      getRoleLists().then((res) => {
        if (res.code === 200) {
        }
      });
    },
    close(reload) {
      if (reload == "reload") {
        this.$confirm(
          `此操作将${this.newForm.id ? "修改" : "添加"}角色信息, 是否继续?`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            if (this.newForm.id) {
              updateRoleInfo(this.newForm.id, {
                ...this.newForm,
              }).then((res) => {
                if (res.code == 200) {
                  this.$message.success(res.message);
                  this.$emit("close", reload);
                }
              });
            } else {
              addRoleInfo(this.newForm).then((res) => {
                if (res.code == 200) {
                  this.$message.success(res.message);
                  this.$emit("close", reload);
                }
              });
            }
          })
          .catch(() => {});
      } else {
        this.$emit("close", reload);
      }
    },
  },
  created() {
    this.fetchRoleLists();
  },
};
</script>

<style lang="less" scoped>
</style>